package com.yanxinim.cn.store.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.yanxinim.cn.R;
import com.yanxinim.cn.Reporter;
import com.yanxinim.cn.bean.RoomMember;
import com.yanxinim.cn.helper.AvatarHelper;
import com.yanxinim.cn.store.Goods;
import com.yanxinim.cn.util.ImageLoadTool;
import com.yanxinim.cn.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class RedPacketAdapter extends RecyclerView.Adapter<RedPacketAdapter.ViewHolder> {

    private Context mContext;
    private List<RoomMember> goodsTypeItemsInfos = new ArrayList<>();
    private List<RoomMember> selectedList = new ArrayList<>();
    private AdapterOnClickListener adapterOnClickListener;
    private int lastSelected = -1;
    public AdapterOnClickListener getAdapterOnClickListener() {
        return adapterOnClickListener;
    }


    public void setAdapterOnClickListener(AdapterOnClickListener adapterOnClickListener) {
        this.adapterOnClickListener = adapterOnClickListener;
    }

    public RedPacketAdapter(Context mContext, List<RoomMember> goodsTypeItemsInfos) {
        this.mContext = mContext;
        this.goodsTypeItemsInfos = goodsTypeItemsInfos;
    }

    public void addAllData(List<RoomMember> goodsItems) {
        goodsTypeItemsInfos.addAll(goodsItems);
        notifyItemRangeChanged(goodsTypeItemsInfos.size() - goodsItems.size(), goodsItems.size());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.row_room_all_member_red_packet, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") int i) {
        RoomMember roomMember = goodsTypeItemsInfos.get(i);
        if (roomMember.isSelected()){
            viewHolder.ivCheck.setImageResource(R.drawable.bg_radio_blu);
        }else {
            viewHolder.ivCheck.setImageResource(R.drawable.bg_radio_no);
        }
        AvatarHelper.getInstance().displayAvatar(roomMember.getUserName(), roomMember.getUserId(), viewHolder.avatarImg, true);
        switch (roomMember.getRole()) {
            case RoomMember.ROLE_OWNER:
                viewHolder.roles.setText(mContext.getString(R.string.group_owner));
                ViewCompat.setBackgroundTintList(viewHolder.roles, ColorStateList.valueOf(mContext.getResources().getColor(R.color.color_role1)));
                break;
            case RoomMember.ROLE_MANAGER:
                viewHolder.roles.setText(mContext.getString(R.string.group_manager));
                ViewCompat.setBackgroundTintList(viewHolder.roles, ColorStateList.valueOf(mContext.getResources().getColor(R.color.color_role2)));
                break;
            case RoomMember.ROLE_MEMBER:
                viewHolder.roles.setText(mContext.getString(R.string.group_role_normal));
                ViewCompat.setBackgroundTintList(viewHolder.roles, ColorStateList.valueOf(mContext.getResources().getColor(R.color.color_role3)));
                break;
            case RoomMember.ROLE_INVISIBLE:
                viewHolder.roles.setText(R.string.role_invisible);
                ViewCompat.setBackgroundTintList(viewHolder.roles, ColorStateList.valueOf(mContext.getResources().getColor(R.color.color_role4)));
                break;
            case RoomMember.ROLE_GUARDIAN:
                viewHolder.roles.setText(R.string.role_guardian);
                ViewCompat.setBackgroundTintList(viewHolder.roles, ColorStateList.valueOf(mContext.getResources().getColor(R.color.color_role5)));
                break;
            default:
                Reporter.unreachable();
                viewHolder.roles.setVisibility(View.GONE);
                break;
        }
        viewHolder.userNameTv.setText(roomMember.getUserName());
        viewHolder.itemView.setOnClickListener(v -> adapterOnClickListener.onClick(i, roomMember));
    }

    public void updateItem(int position, RoomMember roomMember){
        if (position != lastSelected){
            if (lastSelected != -1){
                RoomMember roomMember1 =  goodsTypeItemsInfos.get(lastSelected);
                roomMember1.setSelected(false);
                notifyItemChanged(lastSelected,roomMember1);
            }

            roomMember.setSelected(true);
            notifyItemChanged(position,roomMember);
            lastSelected = position;
        }
    }

    public interface AdapterOnClickListener {
        void onClick(int position, RoomMember data);
    }

    public interface SelectedListener {
        void selected(List<RoomMember> goods);
    }

    @Override
    public int getItemCount() {
        return goodsTypeItemsInfos.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.catagory_title)
        TextView catagoryTitle;
        @BindView(R.id.iv_check)
        ImageView ivCheck;
        @BindView(R.id.avatar_img)
        CircleImageView avatarImg;
        @BindView(R.id.roles)
        TextView roles;
        @BindView(R.id.user_name_tv)
        TextView userNameTv;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
